using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;


namespace WebLib
{
    public class BlockTable
    {
        private string bannerText;
        private string[] columnTitles;
        private List<Row> rows = new List<Row>();


        public BlockTable(params string[] columnTitles)
        {
            this.columnTitles = columnTitles;
        }


        public Row AddRow()
        {
            Row row = new Row(this.columnTitles.Length);
            this.rows.Add(row);
            return row;
        }


        public void Write(HtmlTextWriter writer)
        {
            // Start the div and table...
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "block");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "block_table");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            // writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            // Banner, if any...
            if (this.bannerText != null)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute(HtmlTextWriterAttribute.Colspan, this.columnTitles.Length.ToString());
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "block_banner_cell");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(this.BannerText);
                writer.RenderEndTag();
                writer.RenderEndTag();
            }

            // Column titles...
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            foreach (string title in this.columnTitles)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "block_header_cell");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(title);
                writer.RenderEndTag();
            }
            writer.RenderEndTag();

            // Main body...
            foreach (Row row in this.rows)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                foreach (string item in row.Columns)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "block_cell");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.Write(string.IsNullOrEmpty(item) ? "&nbsp" : item);
                    writer.RenderEndTag();
                }

                writer.RenderEndTag();
            }

            // Close off the table and div.
            writer.RenderEndTag();
            writer.RenderEndTag();
        }


        public string BannerText
        {
            get { return this.bannerText; }
            set { this.bannerText = value; }
        }


        public class Row
        {
            string[] columns;

            public Row(int numColumns)
            {
                columns = new string[numColumns];
            }


            public string this[int idx]
            {
                get { return this.columns[idx]; }
                set { this.columns[idx] = value; }
            }


            public string[] Columns
            {
                get { return this.columns; }
            }
        }

    }
}
